package com.health.db.service.user.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.health.db.dao.order.IRegisterOrderZScheduleDao;
import com.health.db.dao.user.IStuffListDao;
import com.health.db.dao.user.IStuffScheduleDao;
import com.health.db.dto.DoctorListDto;
import com.health.db.model.RegisterOrderZScheduleTemp;
import com.health.db.model.StuffListExample;
import com.health.db.model.StuffSchedule;
import com.health.db.model.StuffScheduleTemp;
import com.health.db.service.user.IStuffScheduleService;
import com.health.mapp.utils.DateUtils;

public class StuffScheduleServiceImpl implements IStuffScheduleService {
	
	private IStuffScheduleDao stuffScheduleDao;
	
	private IRegisterOrderZScheduleDao registerOrderZScheduleDao;

	private IStuffListDao stuffListDao;
	
	/**
	 * @return the stuffListDao
	 */
	public IStuffListDao getStuffListDao() {
		return stuffListDao;
	}

	/**
	 * @param stuffListDao the stuffListDao to set
	 */
	public void setStuffListDao(IStuffListDao stuffListDao) {
		this.stuffListDao = stuffListDao;
	}

	/**
	 * @return the registerOrderZScheduleDao
	 */
	public IRegisterOrderZScheduleDao getRegisterOrderZScheduleDao() {
		return registerOrderZScheduleDao;
	}

	/**
	 * @param registerOrderZScheduleDao the registerOrderZScheduleDao to set
	 */
	public void setRegisterOrderZScheduleDao(
			IRegisterOrderZScheduleDao registerOrderZScheduleDao) {
		this.registerOrderZScheduleDao = registerOrderZScheduleDao;
	}

	public IStuffScheduleDao getStuffScheduleDao() {
		return stuffScheduleDao;
	}

	public void setStuffScheduleDao(IStuffScheduleDao stuffScheduleDao) {
		this.stuffScheduleDao = stuffScheduleDao;
	}

	
	@Override
	public List<StuffScheduleTemp> selectByStuff(Long stuffId){
		
		List<StuffScheduleTemp> returnList = new ArrayList<StuffScheduleTemp>();
		
		//查询医生预约表
		List<StuffSchedule> stuffScheduleList= stuffScheduleDao.selectByStuff(stuffId);
		
		Map<Integer,StuffSchedule> mapStuffSchedule = new HashMap<Integer,StuffSchedule>();
		
		//后续遍历，塞入map 便于查询
		for (int i = 0; i < stuffScheduleList.size(); i++) {
			StuffSchedule schedule = stuffScheduleList.get(i);
			mapStuffSchedule.put(schedule.getWeekDay().intValue(), schedule);
		}
		
		//星期（周日~周六 0~6）
		int today = DateUtils.dayOfWeek();
		Date now = DateUtils.now();
		//遍历日期
		Map<String,StuffScheduleTemp> mapStuffScheduleTemp = new HashMap<String,StuffScheduleTemp>();
		for (int i = 0; i < 7; i++) {
			StuffScheduleTemp temp = new StuffScheduleTemp();
		
			//getDateAfter
			int day = (today+i-1)%7;
			//int day = (today+i-1)%7;
			temp.setWeekDay(day);
			temp.setOrderDate(DateUtils.formatDate(DateUtils.getDateAfter(now, i)));
			StuffSchedule schedule =(StuffSchedule) mapStuffSchedule.get(day);
			if (null!=schedule) {
				temp.setOrderTimeSolts(schedule.getSolts());
			}else{
				mapStuffScheduleTemp.put(temp.getOrderDate(), temp);
				continue;
			}
			//当天预约两小时以后
			if(i == 0){
				String[] slotsStr = schedule.getSolts().split(",");
				int top =DateUtils.currentTimeSolt()+4;
				
				StringBuffer stringBuffer = new StringBuffer();
				
				for (int k = 0; k < slotsStr.length; k++) {
					
					if (top>=Integer.valueOf(slotsStr[k])) {
						stringBuffer.append(slotsStr[k]).append(",");
					}

				}
				//过期时间段
				//temp.setOrderTimeNoFreeSolts(stringBuffer.toString());
				temp.setOrderTimeSolts(temp.getOrderTimeSolts().replace(stringBuffer.toString(), ""));
			}
			mapStuffScheduleTemp.put(temp.getOrderDate(), temp);
		}
		
		//查询已经预约的
		List<RegisterOrderZScheduleTemp>  zScheduledsList = registerOrderZScheduleDao.selectByStuffId(stuffId);
		
		for (int i = 0; i < zScheduledsList.size(); i++) {
			RegisterOrderZScheduleTemp zScheduled = zScheduledsList.get(i);
			
			if (zScheduled.getAmount()>=2) {
				String date = zScheduled.getScheduleDate();
				StuffScheduleTemp temp = (StuffScheduleTemp) mapStuffScheduleTemp.get(date);
			
				((StuffScheduleTemp) mapStuffScheduleTemp.get(zScheduled.getScheduleDate())).setOrderTimeNoFreeSolts(
						temp.getOrderTimeNoFreeSolts()+zScheduled.getOrderTimeSolt()+",");
			}
		}
		
		//map 转arrayList
		for (int i = 0; i < 7; i++) {
			  	StuffScheduleTemp temp =  (StuffScheduleTemp) mapStuffScheduleTemp.get(DateUtils.formatDate(DateUtils.getDateAfter(now, i)));
			    String orderTimeSolts =temp.getOrderTimeSolts();  //总预约时间节点
				
				String orderTimeNoFreeSolts =temp.getOrderTimeNoFreeSolts();  //可以预约时间节点
			    
			    if (StringUtils.isNotBlank(orderTimeSolts)) {
			    	temp.setOrderTimeSolts(orderTimeSolts.substring(0, orderTimeSolts.length()-1));
				}
			    if (StringUtils.isNotBlank(orderTimeNoFreeSolts)) {
			    	temp.setOrderTimeNoFreeSolts(orderTimeNoFreeSolts.substring(0, orderTimeNoFreeSolts.length()-1));
				}
			    
			   returnList.add(temp);
		
		}
		return returnList;
	}
	
	
	@Override
	public List<StuffScheduleTemp> selectAllStuff(){
		
		List<StuffScheduleTemp> returnList = new ArrayList<StuffScheduleTemp>();
		
		//查询医生预约表
		List<StuffSchedule> stuffScheduleList= stuffScheduleDao.selectByStuff(null);
		
		StringBuilder buff0 = new StringBuilder();
		StringBuilder buff1 = new StringBuilder();
		StringBuilder buff2 = new StringBuilder();
		StringBuilder buff3 = new StringBuilder();
		StringBuilder buff4= new StringBuilder();
		StringBuilder buff5 = new StringBuilder();
		StringBuilder buff6 = new StringBuilder();
		
		
		
		Map<String,StuffScheduleTemp> mapStuffScheduleTemp = new HashMap<String,StuffScheduleTemp>();
		
		int today = DateUtils.dayOfWeek()-1;
		Date now = DateUtils.now();
		
		for (int i = 0; i < stuffScheduleList.size(); i++) {
		
			StuffSchedule schedule = stuffScheduleList.get(i);
			if (null!=schedule.getWeekDay()) {
			int day = schedule.getWeekDay();
			String solts=schedule.getSolts();
			switch (day) {
			case 1:buff1.append(solts); break;
			case 2:buff2.append(solts); break;
			case 3:buff3.append(solts); break;
			case 4:buff4.append(solts); break;
			case 5:buff5.append(solts); break;
			case 6:buff6.append(solts); break;
			case 7:buff0.append(solts); break;
			default:
				break;
			}
			}
		}
		
		StringBuilder[] buff = {buff0,buff1,buff2,buff3,buff4,buff5,buff6};
		for (int i = 0; i < 7; i++) {
			
		    StuffScheduleTemp temp = new StuffScheduleTemp();
		    int day = (today+i)%7;
		 	temp.setWeekDay(day);
		 	int offset =day-today;
		 	if (offset<0) {
		 		offset = offset+7;
		 	}

		 	temp.setOrderDate(DateUtils.formatDate(DateUtils.getDateAfter(now, offset)));
			//buff[day]第几天的数据
		 	if (null!=buff[day]&&buff[day].length()>0) {
		
				String[] strs = buff[day].toString().split(",");
				Set<Integer> set = new HashSet<Integer>();
				Map<String,Integer> map = new HashMap<String, Integer>();
	
				//去除过期数据
				if (i==0) {
					int top =DateUtils.currentTimeSolt()+4;
					for (int j = 0; j < strs.length; j++) {
						Integer solt=Integer.valueOf(strs[j]);
						if (top<solt) {
							set.add(solt);
							if(null!= map.get(String.valueOf(solt))){
								map.put(String.valueOf(solt), map.get(String.valueOf(solt))+1); //value+1
							}else{
								map.put(String.valueOf(solt),1);
							}
						}
					}
				}else{
					for (int j = 0; j < strs.length; j++) {
						Integer solt=Integer.valueOf(strs[j]);
						set.add(solt);
						if(null!= map.get(String.valueOf(solt))){
							map.put(String.valueOf(solt), map.get(String.valueOf(solt))+1); //value+1
						}else{
							map.put(String.valueOf(solt),1);
						}
					}
				}
				 List<Integer> list = new ArrayList<Integer>(set);
				 //排序
			     Collections.sort(list);
				
			     //list to String 
			 	 temp.setOrderTimeSolts(JSONArray.toJSONString(list).replace("[", "").replace("]", ""));
			     temp.setMap(map);
			}else{
				 temp.setOrderTimeSolts("");
				 temp.setOrderTimeNoFreeSolts("");
			}
			 mapStuffScheduleTemp.put(temp.getOrderDate(), temp);
		}
		
		//查询已经预约的
		List<RegisterOrderZScheduleTemp>  zScheduledsList = registerOrderZScheduleDao.selectByStuffId(null);
		
		for (int i = 0; i < zScheduledsList.size(); i++) {
			RegisterOrderZScheduleTemp zScheduled = zScheduledsList.get(i);
			String date = zScheduled.getScheduleDate();
			StuffScheduleTemp temp = (StuffScheduleTemp) mapStuffScheduleTemp.get(date);
			Map map = temp.getMap();
		
			if (null!= map.get(String.valueOf(zScheduled.getOrderTimeSolt()))) {
				int size=(Integer) map.get(String.valueOf(zScheduled.getOrderTimeSolt()))*2;
				if (zScheduled.getAmount()>=size) {
					
					((StuffScheduleTemp) mapStuffScheduleTemp.get(zScheduled.getScheduleDate())).setOrderTimeNoFreeSolts(
							temp.getOrderTimeNoFreeSolts()+zScheduled.getOrderTimeSolt()+",");
				}
			}
			
		 }
		
		for (int i = 0; i < 7; i++) {
		  	StuffScheduleTemp temp =  (StuffScheduleTemp) mapStuffScheduleTemp.get(DateUtils.formatDate(DateUtils.getDateAfter(now, i)));
		    String orderTimeSolts =temp.getOrderTimeSolts();  //总预约时间节点
			
			String orderTimeNoFreeSolts =temp.getOrderTimeNoFreeSolts();  //可以预约时间节点
		    
		    if (StringUtils.isNotBlank(orderTimeSolts)) {
		    	temp.setOrderTimeSolts(orderTimeSolts.substring(0, orderTimeSolts.length()));
			}
		    if (StringUtils.isNotBlank(orderTimeNoFreeSolts)) {
		    	temp.setOrderTimeNoFreeSolts(orderTimeNoFreeSolts.substring(0, orderTimeNoFreeSolts.length()));
			}
		    temp.setMap(null);
		   returnList.add(temp);
	
		}
		return returnList;
	}

	@Override
	public List<DoctorListDto> selectDocList( StuffListExample example) {
		// TODO Auto-generated method stub
		List<DoctorListDto> list = stuffListDao.selectByQueryDocReq(example);
		
		 Map<String,DoctorListDto> map=new HashMap<String,DoctorListDto>();//list转map
         
		 for (int i = 0; i < list.size(); i++) {
			 DoctorListDto dto = list.get(i);
			 map.put(dto.getDocId(), dto);
		 }
		 
		//查询已经预约的
			List<RegisterOrderZScheduleTemp>  zScheduledsList = registerOrderZScheduleDao.selectByAllStuffId(Integer.valueOf(
					example.getSolt().replace(",", "")));
			
			for (int i = 0; i < zScheduledsList.size(); i++) {
				RegisterOrderZScheduleTemp zScheduled = zScheduledsList.get(i);
				
				if (zScheduled.getAmount()>=2) {
					//String date = zScheduled.getScheduleDate();
					map.remove(String.valueOf(zScheduled.getAccepterid()));
				}
			}
			list.clear();
			list =new ArrayList<DoctorListDto>(map.values()); 
			
		return list;
	}
	
	
}
